<?php
/*
 *  ┌─────────────────────────────────────────────────────────────┐
 *  │┌───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┐│
 *  ││Esc│!1 │@2 │#3 │$4 │%5 │^6 │&7 │*8 │(9 │)0 │_- │+= │|\ │`~ ││
 *  │├───┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴───┤│
 *  ││ Tab │ Q │ W │ E │ R │ T │ Y │ U │ I │ O │ P │{[ │}] │ BS  ││
 *  │├─────┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴─────┤│
 *  ││ Ctrl │ A │ S │ D │ F │ G │ H │ J │ K │ L │: ;│" '│ Enter  ││
 *  │├──────┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴────┬───┤│
 *  ││ Shift  │ Z │ X │ C │ V │ B │ N │ M │< ,│> .│? /│Shift │Fn ││
 *  │└─────┬──┴┬──┴──┬┴───┴───┴───┴───┴───┴──┬┴───┴┬──┴┬─────┴───┘│
 *  │      │Fn │ Alt │         Space         │ Alt │Win│   HHKB   │
 *  │      └───┴─────┴───────────────────────┴─────┴───┘          │
 *  └─────────────────────────────────────────────────────────────┘
 * 
 * @Date         : 2020-09-13 12:41:26
 * @Author       : 真友
 * @Description  : 本代码由【真友】大力敲打
 */

namespace app\common;
use think\facade\Db;
use \app\common\Time;
class Reward
{
  //订单奖金结算
  public function run($order = '')
  {
    $sys = getSettingType('会员等级');
    $pay_time = $order['pay_time'];
    $user_reward1 = $sys['user_reward1'];
    $user_reward2 = $sys['user_reward2'];
    $user_reward3 = $sys['user_reward3'];
    $order_sn = $order['order_sn'];
    $order_status = $order['status'];
    $source = $order['source'];
    $reward = getSettingType('会员等级');
    $app_take = $reward['app_take'];
    //总佣金
    $comm_total_price = $order['commission_price'];
    if (!$comm_total_price) return;
    //平台抽成 app_take
    $comm_sys_price = $comm_total_price * $app_take;
    $comm_sys_price = bcadd($comm_sys_price,0,2);
    //余下的会员分
    $total_user_price = $comm_total_price - $comm_sys_price;


    //平台抽成 入库
    $vv = array();
    $vv['order_sn'] =$order_sn;
    $vv['source'] =$source;
    $vv['total_fee'] = $comm_total_price;
    $vv['pay_time'] =$pay_time;
    $vv['ctime'] = time();
    $vv['order_status'] = $order_status;
    
    $ck = Db::name('reward')
              ->where('order_sn',$order_sn)
              ->where('level',0)
              ->where('source',$source)
              ->where('type','平台抽成')
              ->find();
    if (!$ck) {
      $vv['ratio'] = $app_take;
      $vv['type'] = '平台抽成';
      $vv['level'] = 0;
      $vv['fee'] = $comm_sys_price;
      $vv['remark'] = '平台抽成';
      Db::name('reward')->insert($vv);
    }else{
      $u = array();
      $u['order_status'] = $order_status;
      $u['fee'] = $comm_sys_price;
      Db::name('reward')->where('id',$ck['id'])->update($u);
    }


    //会员分成

    // 我的分成 
    $my_reward = 1 - $user_reward3 - $user_reward2 - $user_reward1;
    $user = Db::name('user')->where('id',$order['uid'])->find();
    if (!$user) return;
    $my_reward_fee = $total_user_price*$my_reward;
    $my_reward_fee = round($my_reward_fee,2);

    $ck = Db::name('reward')
              ->where('order_sn',$order_sn)
              ->where('uid',$order['uid'])
              ->where('to_uid',$user['id'])
              ->where('level',0)
              ->where('source',$source)
              ->where('type','自购')
              ->find();
    if (!$ck) {
      $vv['uid'] = $order['uid'];
      $vv['to_uid'] = $user['id'];
      $vv['ratio'] = $app_take;
      $vv['type'] = '自购';
      $vv['level'] = 0;
      $vv['fee'] = $my_reward_fee;
      $vv['remark'] = '自购';
      Db::name('reward')->insert($vv);
    }else{
      $u = array();
      $u['order_status'] = $order_status;
      $u['fee'] = $my_reward_fee;
      Db::name('reward')->where('id',$ck['id'])->update($u);
    }

    // if (!$total_user_price) return;
    // 一级分成 
    $user1 = Db::name('user')->where('id',$user['pid'])->find();
    if (!$user1) return;
    $user_reward1_fee = $total_user_price*$user_reward1;
    $user_reward1_fee = round($user_reward1_fee,2);

    $ck = Db::name('reward')
              ->where('order_sn',$order_sn)
              ->where('uid',$order['uid'])
              ->where('to_uid',$user1['uid'])
              ->where('level',1)
              ->where('source',$source)
              ->find();
    if (!$ck) {
      $vv['uid'] = $order['uid'];
      $vv['to_uid'] = $user1['id'];
      $vv['ratio'] = $user_reward1;
      $vv['type'] = '一级';
      $vv['level'] = 1;
      $vv['fee'] = $user_reward1_fee;
      Db::name('reward')->insert($vv);
    }else{
      $u = array();
      $u['order_status'] = $order_status;
      $u['fee'] = $user_reward1_fee;
      Db::name('reward')->where('id',$ck['id'])->update($u);
    }

    // 二级分成 
    $user2 = Db::name('user')->where('id',$user1['pid'])->find();
    if (!$user2) return;
    $user_reward2_fee = $total_user_price*$user_reward2;
    $user_reward2_fee = round($user_reward2_fee,2);

    $ck = Db::name('reward')
              ->where('order_sn',$order_sn)
              ->where('uid',$order['uid'])
              ->where('to_uid',$user2['uid'])
              ->where('level',2)
              ->where('source',$source)
              ->find();
    if (!$ck) {
      $vv['uid'] = $order['uid'];
      $vv['to_uid'] = $user2['id'];
      $vv['ratio'] = $user_reward2;
      $vv['type'] = '二级';
      $vv['level'] = 2;
      $vv['fee'] = $user_reward2_fee;
      Db::name('reward')->insert($vv);
    }else{
      $u = array();
      $u['order_status'] = $order_status;
      $u['fee'] = $user_reward2_fee;
      Db::name('reward')->where('id',$ck['id'])->update($u);
    }


    // 三级分成 
    $user3 = Db::name('user')->where('id',$user2['pid'])->find();
    if (!$user3) return;
    $user_reward3_fee = $total_user_price*$user_reward3;
    $user_reward3_fee = round($user_reward3_fee,2);

    $ck = Db::name('reward')
              ->where('order_sn',$order_sn)
              ->where('uid',$order['uid'])
              ->where('to_uid',$user3['uid'])
              ->where('level',3)
              ->where('source',$source)
              ->find();
    if (!$ck) {
      $vv['uid'] = $order['uid'];
      $vv['to_uid'] = $user3['id'];
      $vv['ratio'] = $user_reward3;
      $vv['type'] = '三级';
      $vv['level'] = 3;
      $vv['fee'] = $user_reward3_fee;
      Db::name('reward')->insert($vv);
    }else{
      $u = array();
      $u['order_status'] = $order_status;
      $u['fee'] = $user_reward3_fee;
      Db::name('reward')->where('id',$ck['id'])->update($u);
    }


  }
  //平台抽成
  private function appTake($order = array(),$setting = array()){
    //
  }
  //一级
  private function level1($order = array(),$setting = array()){
    //
  }
}